<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import = "java.util.*" import="cart.hibernate.paymentMethod.*" import="cart.hibernate.order.*" import="cart.*,java.net.*,java.text.*" %>
<%@page import="cart.hibernate.orderStatus.*" 
        import="cart.hibernate.address.*" 
        import="cart.hibernate.member.*"  %>

<jsp:useBean id = "paymentMethodManager" scope= "request" class = "cart.hibernate.paymentMethod.ManagePaymentMethod" />
<jsp:useBean id = "orderManager" scope= "request" class = "cart.hibernate.order.ManageOrder" />
<jsp:useBean id = "orderStatusManager" scope= "request" class = "cart.hibernate.orderStatus.ManageOrderStatus" />
<jsp:useBean id = "addressManager" scope= "request" class = "cart.hibernate.address.ManageAddress" />
<jsp:useBean id = "memberManager" scope= "request" class = "cart.hibernate.member.ManageMember" />
<jsp:useBean id = "movieManager" scope= "request" class = "cart.hibernate.movieInformation.ManageMovieInformation" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Manage Order</h1>

        <!-- Table with integrated management -->
        <table border="0" style="border-color:black;margin-bottom:20px;">
                <tr>
                    <th>Order ID</th>
                    <th>Member ID</th>
                    <th>Order Datetime</th>
                    <th>Shipped Datetime</th>
                    <th>Pickup Person Name</th>
                    <th>Modified Datetime</th>
                    <th>Credit Card Number</th>
                    <th>Total Price</th>
                    <th>Payment Method</th>
                    <th>Order Status</th>
                    <th>Address</th>
                    <th>Ordered Items</th>
                    
                    <th>Update?</th>
                    <th>Delete?</th>
                </tr>
                <%
                    List orderList = orderManager.getAllOrder();
                    List paymentMethodList = paymentMethodManager.getAllPaymentMethod();
                    List orderStatusList = orderStatusManager.getAllOrderStatus();
                    List addressList = addressManager.getAllAddress();
                    List memberList = memberManager.getAllMember();
                    List movieList = movieManager.getAllMovie();


                    Iterator iterOrder = orderList.iterator();
                    Iterator iterPaymentMethod = paymentMethodList.iterator();
                    Iterator iterOrderStatus = orderStatusList.iterator();
                    Iterator iterAddress = addressList.iterator();
                    Iterator iterMember = memberList.iterator();
                    Iterator iterMovie = movieList.iterator();

                    while (iterOrder.hasNext())
                    {

                        Order order = (Order) iterOrder.next();
                        if(order.getMember().getLoginName() != request.getParameter("id"))
                            continue;
                %>
                        <tr>
                        <table>
                            <form action="ManageOrderServlet" method="post">
                                <input type="hidden" name="orderId" value="<%= order.getOrderId() %>"> 
                                <td>
                                    <%= order.getOrderId() %>
                                </td>
                                <td>
                                    <input type="date" name="orderDatetime" value="<%= order.getOrderDatetime() %>" />
                                </td>
                                <td>
                                    <input type="date" name="shippedDatetime" value="<%= order.getShippedDatetime() %>" />
                                </td>
                                <td>
                                    <input type="text" name="pickupPersonName" value="<%= order.getPickupPersonName() %>" />
                                </td>
                                <td>
                                    <%= order.getModifiedDatetime() %>
                                </td>
                                <td>
                                    <input type="string" name="creditCardNum" value="<%= order.getCreditCardNum() %>" />
                                </td>
                                <td>
                                    <input type="text" name="totalPrice" value="<%= order.getTotalPrice() %>" />
                                </td>
                                <td>
                                    <select name="paymentMethodId">
                                        <%
                                            while (iterPaymentMethod.hasNext())
                                            {
                                                PaymentMethod paymentMethod = (PaymentMethod) iterPaymentMethod.next();

                                                if(paymentMethod.getPaymentMethodId() == order.getPaymentMethod().getPaymentMethodId())
                                                    out.print("<option value='" + paymentMethod.getPaymentMethodId() + "' selected='selected'>" + paymentMethod.getPaymentMethod() + "</option>");
                                                else
                                                    out.print("<option value='" + paymentMethod.getPaymentMethodId() + "'>" + paymentMethod.getPaymentMethod() + "</option>");
                                            }
                                            iterPaymentMethod = paymentMethodList.iterator();
                                        %>
                                    </select>
                                </td>

                                <td>
                                    <select name="orderStatusId">
                                        <%
                                            while (iterOrderStatus.hasNext())
                                            {
                                                OrderStatus orderStatus = (OrderStatus) iterOrderStatus.next();

                                                if(orderStatus.getOrderStatusId() == order.getOrderStatus().getOrderStatusId())
                                                    out.print("<option value='" + orderStatus.getOrderStatusId() + "' selected='selected'>" + orderStatus.getOrderStatus() + "</option>");
                                                else
                                                    out.print("<option value='" + orderStatus.getOrderStatusId() + "'>" + orderStatus.getOrderStatus() + "</option>");
                                            }
                                            iterOrderStatus = orderStatusList.iterator();
                                        %>
                                    </select>
                                </td>

                                <td>
                                    <input type="text" name="flat" value="<%= order.getAddress().getFlat() %>" />
                                    <input type="text" name="buildingAndBlock" value="<%= order.getAddress().getBuildingAndBlock() %>" />
                                    <input type="text" name="district" value="<%= order.getAddress().getDistrict() %>" />
                                    <input type="text" name="region" value="<%= order.getAddress().getRegion() %>" />
                                </td>

                                <td>
                                    <select name="loginName">
                                        <%
                                            while (iterMember.hasNext())
                                            {
                                                Member member = (Member) iterMember.next();

                                                if(member.getLoginName() == order.getMember().getLoginName())
                                                    out.print("<option value='" + member.getLoginName() + "' selected='selected'>" + member.getLoginName() + "</option>");
                                                else
                                                    out.print("<option value='" + member.getLoginName() + "'>" + member.getLoginName() + "</option>");
                                            }
                                            iterMember = memberList.iterator();
                                        %>
                                    </select>
                                </td>
                                <td>
                                    <select name="movieId" multiple="multiple">
                                        <%
                                            while (iterMember.hasNext())
                                            {
                                                Member member = (Member) iterMember.next();

                                                if(member.getLoginName() == order.getMember().getLoginName())
                                                    out.print("<option value='" + member.getLoginName() + "' selected='selected'>" + member.getLoginName() + "</option>");
                                                else
                                                    out.print("<option value='" + member.getLoginName() + "'>" + member.getLoginName() + "</option>");
                                            }
                                            iterMember = memberList.iterator();
                                        %>
                                    </select>
                                </td>
                                






                                <td>
                                    <input type="submit" name="submit" value="update">
                                </td>
                                <td>
                                    <input type="submit" name="submit" value="delete">
                                </td>
                            </form>
                        </table>
                        </tr>
                <%        
                    }
                %>
                    <tr>
                    <!-- <table>
                        <form action="ManageOrderServlet" method="post">
                            <td>Enter new order : </td>
                            <td><input type="text" name="loginName" id="loginName"></td>
                            <td><input type="text" name="password" ></td>
                            <td><input type="text" name="email"/>
                            <td>
                                <select name="gender">
                                    <option value="male">M</option>
                                    <option value="female">F</option>
                                </select>
                            </td>
                            <td><input type="text" name="homePhoneNumber" /></td>
                            <td><input type="text" name="cellPhoneNumber" /></td>
                            <td><input type="date" name="birthDate" /></td>
                            <td><input type="date" name="joinDate" /></td>
                            <td><input type="text" name="nickName"  /></td>
                            <td><input type="text" name="point"  /></td>
                            <td><input type="text" name="firstName"  /></td>
                            <td><input type="text" name="middleName"  /></td>
                            <td><input type="text" name="lastName" /></td>
                            <td>
                                <select name="paymentMethodId">
                                    <%
                                        while (iterPaymentMethod.hasNext())
                                        {
                                            PaymentMethod paymentMethod = (PaymentMethod) iterPaymentMethod.next();
                                            out.print("<option value='" + paymentMethod.getPaymentMethodId() + "'>" + paymentMethod.getPaymentMethod() + "</option>");
                                        }
                                        iterPaymentMethod = paymentMethodList.iterator();
                                    %>
                                </select>
                            </td>
                            <td><input type="text" name="order" /></td>
                            <td><input type="submit" name="submit" value="add"></td>
                        </form>
                    </table> -->
                    </tr>
        </table>
    </body>
</html>